﻿using DataDoc.App.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows.Forms;

namespace DataDoc.App
{
    public partial class MainFrm : Form
    {
        /// <summary>
        /// 线程
        /// </summary>
        private Thread? catchThread = null;
        /// <summary>
        /// 配置文件
        /// </summary>
        IniFileHelper iniFile = new IniFileHelper(Application.StartupPath + "\\config.ini");

        public MainFrm()
        {
            InitializeComponent();
        }

        private void MainFrm_Load(object sender, EventArgs e)
        {
            labelLink.Visible = false;
            toolStripStatusLabel1.Text = "开源从我一起，贡献自己的微薄力量，共创人类命运共同体！";

            // 读取INI文件存储信息
            string connection = iniFile.GetString("DOC", "CONNECTION", "");
            string dbIP = iniFile.GetString("DOC", "DB_IP", "");
            string dbName = iniFile.GetString("DOC", "DB_NAME", "");
            string charset = iniFile.GetString("DOC", "DB_CHARSET", "");
            string account = iniFile.GetString("DOC", "DB_ACCOUNT", "");
            string password = iniFile.GetString("DOC", "DB_PASSWORD", "");

            if (!string.IsNullOrEmpty(connection))
            {
                tbConnection.Text = connection;
            }

            if (!string.IsNullOrEmpty(dbIP))
            {
                tbDbIP.Text = dbIP;
                tbDbName.Text = dbName;
                tbCharset.Text = charset;
                tbAccount.Text = account;
                tbPassword.Text = password;
            }
        }

        /// <summary>
        /// 生成文档
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDoc_Click(object sender, EventArgs e)
        {
            btnDoc.Enabled = false;
            btnDoc.BackColor = Color.LightGray;
            btnDoc.Text = "生成中..";
            Serilog.Log.Information("开始生成 -> " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));

            catchThread = new Thread(new ThreadStart(CreateDoc));
            catchThread.Start();
        }

        /// <summary>
        /// 创建文档
        /// </summary>
        private void CreateDoc()
        {
            var connection = tbConnection.Text.Trim();
            var dbIP = tbDbIP.Text.Trim();
            var dbName = tbDbName.Text.Trim();
            var dbCharset = tbCharset.Text.Trim();
            var account = tbAccount.Text.Trim();
            var password = tbPassword.Text.Trim();
            var projectName = tbProjectName.Text.Trim();
            var version = tbVersion.Text.Trim();

            bool bConnection = false;
            if (!string.IsNullOrEmpty(connection))
            {
                iniFile.WriteValue("DOC", "CONNECTION", connection);
                string[] splitConnection = connection.Split(new char[] { ';' });
                for (int i = 0; i < splitConnection.Length; i++)
                {
                    var value = splitConnection[i].Trim();
                    if (!string.IsNullOrEmpty(value))
                    {
                        string[] splitValue = value.Split(new char[] { '=' });
                        if (!string.IsNullOrEmpty(splitValue[1]))
                        {
                            string new_value = splitValue[1].Trim();
                            if (splitValue[0].ToLower().Contains("server"))
                            {
                                iniFile.WriteValue("DOC", "DB_IP", new_value);
                            }
                            if (splitValue[0].ToLower().Contains("database"))
                            {
                                iniFile.WriteValue("DOC", "DB_NAME", new_value);
                            }
                            if (splitValue[0].ToLower().Contains("charset"))
                            {
                                iniFile.WriteValue("DOC", "DB_CHARSET", new_value);
                            }
                            if (splitValue[0].ToLower().Contains("uid"))
                            {
                                iniFile.WriteValue("DOC", "DB_ACCOUNT", new_value);
                            }
                            if (splitValue[0].ToLower().Contains("pwd"))
                            {
                                iniFile.WriteValue("DOC", "DB_PASSWORD", new_value);
                            }
                        }
                    }
                }
                bConnection = true;
            }

            bool bDBInfo = false;
            if (!string.IsNullOrEmpty(dbIP) && !string.IsNullOrEmpty(dbName) && !string.IsNullOrEmpty(dbCharset) && !string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(password))
            {
                iniFile.WriteValue("DOC", "DB_IP", dbIP);
                iniFile.WriteValue("DOC", "DB_NAME", dbName);
                iniFile.WriteValue("DOC", "DB_CHARSET", dbCharset);
                iniFile.WriteValue("DOC", "DB_ACCOUNT", account);
                iniFile.WriteValue("DOC", "DB_PASSWORD", password);
                bDBInfo = true;
            }

            if (!string.IsNullOrEmpty(projectName))
            {
                iniFile.WriteValue("DOC", "PROJECT_NAME", projectName);
            }

            if (!string.IsNullOrEmpty(version))
            {
                iniFile.WriteValue("DOC", "VERSION", version);
            }

            // 满足其中一个条件即可生成
            if (bConnection || bDBInfo)
            {
                bool bPdf = false;
                if (cbPDF.Checked)
                {
                    bPdf = true;
                }

                if (cbHTML.Checked)
                {
                    DocHtmlHelper.CreateHTML(bPdf);
                }
            }

            btnDoc.Enabled = true;
            btnDoc.BackColor = Color.FromArgb(248, 123, 33);
            btnDoc.Text = "生成文档";

            labelLink.Visible = true;
            string path = Application.StartupPath + "doc";
            toolStripStatusLabel1.Text = "数据库文档已生成成功，存储目录：" + path;
            Serilog.Log.Information("完成 -> " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
        }

        /// <summary>
        /// 打开生成文档所在目录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void labelLink_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string path = Application.StartupPath + "doc";
            System.Diagnostics.Process.Start("explorer.exe", path);
        }
    }
}
